<template>
  <div class="info-dialog">
    <el-dialog
      :visible.sync="visible"
      :title="title"
      :modal-append-to-body="true"
      :append-to-body="true"
      :destroy-on-close="false"
      :modal="true"
      :close-on-click-modal="false"
      top="10vh"
      width="850px"
      @closed="closed"
    >

    <div style="padding: 20px;">




    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="检索">
        <el-input v-model="queryForm.keywords" placeholder="请输入关键字."  />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryForm.status" placeholder="请选择">
          <el-option label="已授权" value="0" />
          <el-option label="未授权" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"  size="mini" :loading="loading" @click="onQuerySubmit">查询</el-button>
        <el-button type="info" icon="el-icon-refresh"  size="mini" :loading="loading" @click="onReset">重置</el-button>
        <el-button v-if="queryForm.status=='1'" type="primary" icon="el-icon-circle-plus-outline" plain  size="mini" :loading="loading" @click="submitForm">分配</el-button>
        <el-button v-if="queryForm.status=='0'"type="danger" icon="el-icon-remove-outline" plain size="mini" :loading="loading" @click="submitForm">取消</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleRoleUserTable"
      :data="tableData"
      border
      empty-text="暂无数据."
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="username"
        label="账号"
        >
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        >
      </el-table-column>

      <el-table-column
        prop="phone"
        label="电话"
        >
      </el-table-column>

      <el-table-column
        prop="deptName"
        label="所属部门">
      </el-table-column>
    </el-table>

    <el-row>
      <el-col :span="24">
        <div align="right" style="margin-top: 0.3125rem;">
          <el-pagination background layout="prev, pager, next" :current-page="page.cur" :page-size="page.limit" :total="page.total" @current-change="handleCurrentChange" />
        </div>
      </el-col>
    </el-row>
 </div>

    </el-dialog>
  </div>
</template>

<script>
  import {
    saveRole,
    findRoleUsers,
    saveRoleUsers
  } from '@/api/system/role'

  import {
    isOk
  } from '@/utils/request'

  export default {
    name: 'RoleUserDialog',
    components: {
    },
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed:{
    },
    watch:{
      properties: {
        handler: function(value) {
          this.visible = value.visible
          if (value.data) {
            this.roleData= value.data
            this.title = '当前角色:'+value.data.name
          } else {
            this.roleData={}
          }
        },
        immediate: true,
        deep: false
      }
    },
    created() {
      this.load();
    },
    data(){
      return {
        visible:false,
        title:'',
        loading:false,
        roleData: {
        },
        queryForm: {
          keywords: undefined,
          status: '0',
          orgId: '',
          roleId:''
        },
        page: {
          cur: 1,
          limit: 50,
          total: 0
        },
        tableData: [],
      }
    },
    methods:{
      onQuerySubmit(event) {
        this.load()
      },
      onReset(event) {
        this.queryForm.keywords=undefined;
        this.queryForm.status='0';
        this.load()
      },
      load(){
        this.loading=true
        findRoleUsers({
          cur: this.page.cur,
          limit: this.page.limit,
          keywords: this.queryForm.keywords,
          roleId: this.roleData.id,
          status: this.queryForm.status
        }).then(rsp => {
          if(isOk(rsp)){
            const _data = rsp.result.list
            this.page.total = rsp.result.totalRow
            this.tableData = _data
            this.loading = false
          }
        }).catch(err => {
          console.log(err);
        })
        .finally(() => {
          this.loading = false
        })
      },
      handleCurrentChange(cur) {
        this.page.cur = cur
        this.load()
      },
      submitForm(){

        let rows = this.$refs.multipleRoleUserTable.selection;
        if (rows.length == 0) {
          if ('0' == this.queryForm.status) {
            this.$message({
              message: '请勾选需要取消的用户！',
              type: 'warning'
            });
            return;
          } else {
            this.$message({
              message: '请勾选需要分配的用户！',
              type: 'warning'
            });
            return;
          }
        }

        let userIds="";
        rows.forEach(row => {
          userIds += row.id + ','
        });
        let msg = '0' == this.queryForm.status ? '确定需要取消所选用户吗？' : '确定需要分配所选用户吗？'
        this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading=true
          saveRoleUsers({
            roleId: this.roleData.id,
            userIds: userIds,
            status: this.queryForm.status
          }).then(response => {
            this.$message({
              message: '0' == this.queryForm.status ? '添加用户成功！取消用户成功！' : '',
              type: 'success',
              duration: 500
            })
            this.load()
            this.loading=false
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
        })

      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }

</script>

<style>
</style>
